package org.nmgyjt.springboot.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.nmgyjt.springboot.entity.ApiRes;
import org.nmgyjt.springboot.entity.organization.Organization;
import org.nmgyjt.springboot.mapper.OrganizationMapper;
import org.nmgyjt.springboot.service.OrganizationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Service
public class OrganizationServiceImpl extends ServiceImpl<OrganizationMapper, Organization> implements OrganizationService {
    @Autowired
    private OrganizationMapper organizationMapper;
    @Resource
    private RedisTemplate<String,String> redisTemplate;

    @Override
    public ApiRes listTree() {
        List resultList = new ArrayList<>();
        try {
            String orgTreeString=redisTemplate.opsForValue().get("orgTree");
            if(!ObjectUtils.isEmpty(orgTreeString)){
                try {
                    List redisArray=JSON.parseArray(orgTreeString);
                    return ApiRes.ok(redisArray);
                } catch (Exception e) {
                    log.error(e.toString());
                }
            }
        } catch (Exception e) {
        }
        QueryWrapper<Organization> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("status","ENABLED");
        queryWrapper.ne("code","01000000000000000");
//        queryWrapper.ne("id","dp-017c8337673c4f12b89441c15e3fe60d");
//        queryWrapper.ne("id","dp-13a3d93558a3444880c199487ab62fba");
//        queryWrapper.ne("id","dp-47d4a3d1b0004185aa3a1cbc999c58fb");
//        queryWrapper.ne("id","dp-5471ad898bd4436eaa183c45143b12d4");
//        queryWrapper.ne("id","dp-fae178301c694d79ae41ffde5ac28213");
        List<Organization> list=organizationMapper.selectList(queryWrapper);
        for (Organization treeNode : list) {
            if ("0".equals(treeNode.getParentId())) {
                resultList.add(treeNode);
            }
            for (Organization node : list) {
                if (node.getParentId().equals(treeNode.getId())
                        &&!node.getId().equals(node.getParentId())) {
                    treeNode.addTreeNode(node);
                }
            }
        }
        String json= JSON.toJSONString(resultList);
        List OrgTree=JSON.parseArray(json);
        try {
            redisTemplate.opsForValue().set("orgTree",json,6,TimeUnit.HOURS);
        } catch (Exception e) {
        }
        return ApiRes.ok(OrgTree);
    }

    @Override
    public ApiRes delRedisByKey(String key) {
        boolean b=redisTemplate.delete(key);
        return ApiRes.ok(b);
    }

    @Override
    public ApiRes listOrg(String id) {
        if(ObjectUtils.isEmpty(id)){
            QueryWrapper<Organization> queryWrapper=new QueryWrapper<>();
            queryWrapper.eq("id","dp-3f90f739227e498bb3505237e6c397bb");
            return ApiRes.ok(organizationMapper.selectList(queryWrapper));
        }else{
            QueryWrapper<Organization> queryWrapper=new QueryWrapper<>();
            queryWrapper.eq("parent_id",id);
            return ApiRes.ok(organizationMapper.selectList(queryWrapper));
        }
    }
}
